<?php

class PhotoController extends Controller {
    protected $allowed = array('administrator' , 'member');

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        if ($this->isAllowed()) {
            if (isset($_GET['ajax']))
                $this->render("photo/ajax_index" , array('photos' => $this->auth->photo));
            else
                $this->render("photo/index" , array('photos' => $this->auth->photo));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function delete($photo_id) {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $this->isAllowed() && $model->auth == $this->auth) {
            $model->delete();
            $this->redirect($this->createUrl('photo/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function ajaxdetail($photo_id)
    {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $model->access_rule == PhotoModel::$ACCESS_EVERY ||
            $model->access_rule == PhotoModel::$ACCESS_LOGIN && $this->auth != null ||
            $model->access_rule == PhotoModel::$ACCESS_FOLLOWER && $this->auth->isFollowing($model->auth) ||
            $this->auth->id == $model->auth->id
                ) {

            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $walls = $model->wall;

            $pagin = new Pagination(count($walls), $this->createUrl("photo/ajaxdetail/" . $model->id));
            $walls = Model::paginaze($walls, $page);

            foreach ($walls as $wall) {
                ?>
                <div class="myWallCommentPhoto"><img src="<?php if($wall->getPoster()->getProfilePicture() != NULL) echo $wall->getPoster()->getProfilePicture()->getUrl();?>" alt="" /></div>
                <div class="myWallCommentPoster"><a href="<?php echo $this->createUrl('profile/view/' . $wall->getPoster()->id);?>"> <?php echo $wall->getPoster()->fullname;?></a></div>
                <div class="myWallCommentContent"><?php echo $wall->content;?></div>
                <?php
            }
        }
    }

    
    public function detail($photo_id) {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $model->access_rule == PhotoModel::$ACCESS_EVERY ||
            $model->access_rule == PhotoModel::$ACCESS_LOGIN && $this->auth != null ||
            $model->access_rule == PhotoModel::$ACCESS_FOLLOWER && $this->auth->isFollowing($model->auth) ||
            $this->auth->id == $model->auth->id
                ) {

            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $walls = $model->wall;

            $pagin = new Pagination(count($walls), $this->createUrl("photo/ajaxdetail/" . $model->id));
            $walls = Model::paginaze($walls, $page);

            $this->render("photo/detail" , array('photo' => $model , 'walls' => $walls , 'pagin' => $pagin));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function edit($photo_id) {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $_POST && $model->auth == $this->auth && $this->isAllowed()) {
            $model->description = $_POST['description'];
            $model->keyword = $_POST['keyword'];
            $model->taken_date = $_POST['date'];
            $model->access_rule = $_POST['access_rule'];
            $model->category = $_POST['category'];
            $model->access_rule = $_POST['access_rule'];
            $model->comment_rule = isset($_POST['comment_rule']) ? PhotoModel::$COMMENT_ALLOWED : PhotoModel::$COMMENT_NOT_ALLOWED;

            $tags = $model->tag;
            $user_tagged = array();
            foreach($tags as $tag){
                $user_tagged[] = $tag->user_id;
            }

            $model->clearTag();
            if (isset($_POST['tags'])){
                foreach ($_POST['tags'] as $user_id => $val) {
                    $user = Model::factory('auth' , $user_id);
                    $model->tag($user);
                    
                    //Tag notification
                    if(!in_array($user_id, $user_tagged)){
                        $newNotif = Model::factory('notification');
                        $newNotif->content  = '<a href="'.$this->createUrl('profile/view/'.$this->auth->id).'"><b>'.$this->auth->fullname.'</b></a> tagged you in <a href="'.$this->createUrl('photo/detail/'.$photo_id).'">this photo</a>';
                        $newNotif->user_id  = $user_id;
                        $newNotif->status   = NotificationModel::$NOTIF_UNREAD;
                        $newNotif->save();
                    }
                }
            }

            $model->save();
            $this->redirect($this->createUrl('photo/detail/' . $photo_id));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function upload() {
        if ($this->isAllowed() && $_FILES && $_POST) {
            //print_r($_FILES);
            $name = $_FILES['photoFile']['name'];
            $tmp_name = $_FILES['photoFile']['tmp_name'];
            $name = $this->auth->id . '_' . $name;
            move_uploaded_file($tmp_name, PhotoModel::$UPLOAD_DIR . $name);

            $model = new PhotoModel();
            $model->description = $_POST['description'];
            $model->keyword = $_POST['keyword'];
            $model->category = $_POST['category'];
            $model->taken_date = $_POST['date'];
            $model->access_rule = $_POST['access_rule'];
            $model->comment_rule = isset($_POST['comment_rule']) ? PhotoModel::$COMMENT_ALLOWED : PhotoModel::$COMMENT_NOT_ALLOWED;
            $model->upload_time = date("Y:m:d H:i:s");
            $model->user_id = $this->auth->id;
            $model->name = $name;
            $model->category = $_POST['category'];
            $model->rating = 0;
            $model->save();

            $fb = new FbModel();
            $fb->postToWall("NEW PHOTO UPLOADED");

            //$this->redirect($this->createUrl('photo/index'));
        }
        else if (isset($_GET['code'])) {
            $model = Model::factoryQuery('photo' , "SELECT * FROM `photo` WHERE `user_id` = ".$this->auth->id." ORDER BY `id` DESC LIMIT 1;");
            //$link = $this->createUrl('photo/detail/' . $model->id);
            $fb = new FbModel();
            $fb->postToWall($this->auth->fullname . ' has just uploaded new photo');

            $this->redirect($this->createUrl('photo/index'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function like($photo_id) {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $model->auth != $this->auth && $this->isAllowed()) {
            $model->rating++;
            $model->save();
            $this->redirect($this->createUrl('photo/detail/' . $photo_id));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function unlike($photo_id) {
        $model = Model::factory('photo' , $photo_id);
        if ($model->loaded() && $model->auth != $this->auth && $this->isAllowed()) {
            $model->rating--;
            $model->save();
            $this->redirect($this->createUrl('photo/detail/' . $photo_id));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function comment($photo_id) {
        $photo = Model::factory('photo' , $photo_id);
        if ($photo->loaded() && $this->isAllowed() && $_POST && $photo->comment_rule = PhotoModel::$COMMENT_ALLOWED && $_POST['comment'] != '') {
            $wall = Model::factory('wall');
            $wall->content = $_POST['comment'];
            $wall->photo_id = $photo->id;
            $wall->poster_id = $this->auth->id;
            $wall->save();
            $this->redirect($this->createUrl('photo/detail/' . $photo->id));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

    public function setprofile($photo_id) {
        $photo = Model::factory('photo' , $photo_id);
        if ($photo->loaded() && $this->isAllowed() && $photo->auth == $this->auth) {
            $this->auth->photo_id = $photo->id;
            $this->auth->save();
            $this->redirect($this->createUrl('photo/index/'));
        }
        else
            $this->showError('You are not allowed to open this page');
    }

}

?>